<template>
  <div class="nav-box">
    <div class="admin-img fl">
      <img class="auto-img" :src="adminInfo.avatar" alt="" />
    </div>
    <div class="username fl">{{adminInfo.userName}}</div>
    <div class="logout fr" @click="logout">退出</div>
  </div>
</template>

<script>
import '@/assets/less/b_adminnav.less'
export default {
  name: 'AdminNav',

  data() {
    return {
      adminInfo: {
        avatar: '',
        userName: ''
      }
    };
  },

  created() {
    //获取用户信息
    this.getAdminInfo();
  },

  methods: {
    //获取用户信息
    getAdminInfo() {
      this.axios({
        method: 'GET',
        url: this.baseUrl + '/getAdminInfo'
      }).then(res => {
        console.log('res ==> ', res);

        if (res.data.code == 1034) {
          this.$message.error(res.data.msg);
          this.$router.push({ name: 'B_Login' });
        }else if (res.data.code == 1040) {
          this.adminInfo.avatar = res.data.data.url + res.data.data.result[0].avatar;
          this.adminInfo.userName = res.data.data.result[0].adminName;
        } else {
          this.$message.error(res.data.msg);
          this.$router.push({ name: 'B_Login' })
        }

      }).catch(err => {
        console.log('err ==> ', err);
        this.$router.push({ name: 'B_Login' })
      })
    },

    //退出登录
    logout() {
      //清除cookie
      this.$cookies.keys().map(cookieName => {
        this.$cookies.remove(cookieName);
      })
      this.$message({
        message: `退出成功`,
        type: 'success'
      });
      this.$router.push({ name: 'B_Login' });

    }
  }
}
</script>